package org.wzy.xmlg_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wzy.xmlg_backend.entity.Dept;
import org.wzy.xmlg_backend.entity.Result;
import org.wzy.xmlg_backend.service.IDeptService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author 位置杨
 * @since 2025-06-10
 */
@RestController
@Tag(name = "DeptController", description = "部门表")
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private IDeptService deptService;

    /**
     * 获取所有部门信息
     *
     * @return 部门列表
     */
    @GetMapping
    @Operation(summary = "获取所有部门")
    public Result<List<Dept>> getAllDepts() {
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    /**
     * 分页查询部门信息
     *
     * @param current 当前页码
     * @param size 每页大小
     * @param name 部门名称（可选）
     * @return 分页结果
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询部门")
    public Result<Page<Dept>> pageDepts(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "部门名称") @RequestParam(required = false) String name) {

        //  创建分页对象
        Page<Dept> page = new Page<>(current, size);
        LambdaQueryWrapper<Dept> queryWrapper = new LambdaQueryWrapper<>();

        //  根据部门名称进行模糊查询
        if (name != null && !name.isEmpty()) {
            queryWrapper.like(Dept::getName, name);
        }
        
        Page<Dept> result = deptService.page(page, queryWrapper);
        return Result.success(result);
    }

    /**
     * 根据ID获取部门信息
     * 
     * @param id 部门ID
     * @return 部门信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取部门")
    public Result<Dept> getDeptById(@PathVariable Integer id) {
        Dept dept = deptService.getById(id);
        if (dept != null) {
            return Result.success(dept);
        } else {
            return Result.fail("部门不存在");
        }
    }

    /**
     * 创建新部门
     * 
     * @param dept 部门信息
     * @return 创建结果
     */
    @PostMapping
    @Operation(summary = "创建部门")
    public Result<Dept> createDept(@RequestBody Dept dept) {
        // 设置创建和更新时间
        LocalDateTime now = LocalDateTime.now();
        dept.setCreateTime(now);
        dept.setUpdateTime(now);

        //  验证部门名称是否已存在
        boolean success = deptService.save(dept);
        if (success) {
            return Result.success( dept);
        } else {
            return Result.fail("创建部门失败");
        }
    }

    /**
     * 更新部门信息
     * 
     * @param id 部门ID
     * @param dept 部门信息
     * @return 更新结果
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新部门")
    public Result<Dept> updateDept(@PathVariable Integer id, @RequestBody Dept dept) {
        Dept existingDept = deptService.getById(id);
        if (existingDept == null) {
            return Result.fail("部门不存在");
        }
        
        dept.setId(id);
        dept.setUpdateTime(LocalDateTime.now());
        dept.setCreateTime(existingDept.getCreateTime()); // 保留原创建时间
        
        boolean success = deptService.updateById(dept);
        if (success) {
            return Result.success(dept);
        } else {
            return Result.fail("更新部门失败");
        }
    }

    /**
     * 删除部门
     * 
     * @param id 部门ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除部门", description = "根据ID删除部门")
    public Result<Void> deleteDept(@PathVariable Integer id) {  // 将Long改为Integer
        boolean exists = deptService.getById(id) != null;
        if (!exists) {
            return Result.fail("部门不存在");
        }

        boolean success = deptService.removeById(id);
        if (success) {
            return Result.success();
        } else {
            return Result.fail("删除部门失败");
        }
    }
}
